package log350.restaurant;

import Class.Livraison;
import Class.SingletonCommandeCourante;
import Class.SingletonCommandesEnCours;
import android.app.Activity;
import android.graphics.Color;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class Livraisons extends Activity {
	private EditText nom;
	private EditText numTel;
	private EditText adresse;
	private EditText appartement;
	private EditText ville;
	private EditText codePostal;
	
	private TextView nomError;
	private TextView numTelError;
	private TextView adresseError;
	private TextView villeError;
	private TextView codePostalError;
	private TextView textCommandeError;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_livraisons);
		  nomError = (TextView)this.findViewById(R.id.textViewLivraisonNomError);
		  numTelError =  (TextView)this.findViewById(R.id.textViewLivraisonNumeroTelephoneError);
		  adresseError =  (TextView)this.findViewById(R.id.textViewLivraisonAdresseError);
		  villeError =  (TextView)this.findViewById(R.id.textViewLivraisonVilleError);
		  codePostalError =  (TextView)this.findViewById(R.id.textViewLivraisonCodePostalError);
		  textCommandeError =  (TextView)this.findViewById(R.id.textCommandeError);
		  
		  if (SingletonCommandeCourante.getCommandeCourante().getCommande().getItems().size() > 0) {
			  textCommandeError.setVisibility(View.GONE);
		  }
		  
		  
		  nomError.setVisibility(View.GONE);
		  numTelError.setVisibility(View.GONE);
		  adresseError.setVisibility(View.GONE);
		  villeError.setVisibility(View.GONE);
		  codePostalError.setVisibility(View.GONE);
		  
		  textCommandeError.setTextColor(Color.RED);
		  nomError.setTextColor(Color.RED);
		  numTelError.setTextColor(Color.RED);
		  adresseError.setTextColor(Color.RED);
		  villeError.setTextColor(Color.RED);
		  codePostalError.setTextColor(Color.RED);
		// Show the Up button in the action bar.
		getActionBar().setDisplayHomeAsUpEnabled(true);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_livraisons, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			NavUtils.navigateUpFromSameTask(this);
			return true;
		case R.id.Factures: {
			if (SingletonCommandeCourante.getCommandeCourante().getCommande().getItems().size() == 0) {
				
				return (false);
			}
			if (!getLivraisonValid()) {
			    return (false);
			}
			Livraison livraison = new Livraison(nom.getText().toString(),adresse.getText().toString() , 
					ville.getText().toString(), codePostal.getText().toString(), numTel.getText().toString(), appartement.getText().toString());
			SingletonCommandeCourante.getCommandeCourante().getCommande().setLivraison(livraison);
			Intent intent = new Intent(this, Factures.class);
			startActivity(intent);
			return (true);
		}
		}

		return super.onOptionsItemSelected(item);
	}
	private Boolean getLivraisonValid(){
		Boolean retour = true;
		numTel = (EditText)this.findViewById(R.id.editTextLivraisonNumeroTelephone);
		codePostal = (EditText) this.findViewById(R.id.editTextLivraisonCodePostal);
		nom =(EditText) this.findViewById(R.id.editTextLivraisonNom);
	    adresse = (EditText) this.findViewById(R.id.editTextLivraisonAdresse);
		appartement = (EditText) this.findViewById(R.id.editTextLivraisonAppartement);
		ville = (EditText) this.findViewById(R.id.editTextLivraisonVille);
		
		if (nom.getText().length() <= 0) {
			nomError.setVisibility(View.VISIBLE);			 
			retour = false;
		}
		else
		{
			nomError.setVisibility(View.GONE);
		}
		
		if (adresse.getText().length() <= 0) {
			adresseError.setVisibility(View.VISIBLE);
			retour = false;
		}
		else
		{
			adresseError.setVisibility(View.GONE);
		}
		
		if (ville.getText().length() <= 0) {
			villeError.setVisibility(View.VISIBLE);
			retour = false;
		}
		else
		{
			villeError.setVisibility(View.GONE);
		}
		
		if (numTel.getText().length() != 14) {
			numTelError.setVisibility(View.VISIBLE);
			retour = false;
		}
		else
		{
			numTelError.setVisibility(View.GONE);
		}
		
		   // String zipCodePattern = "[a-zA-Z]\\d[a-zA-Z]\\d[a-zA-Z]\\d$;";
		  //!codePostal.getText().toString().matches(zipCodePattern)
		if (codePostal.getText().length() != 7) {		
			codePostalError.setVisibility(View.VISIBLE);
			retour = false;
		}
		else
		{
			codePostalError.setVisibility(View.GONE);
		}
		return retour;
	}
	

}
